package com.lucky.ebidding.service;

import com.lucky.ebidding.dao.PublishInfoDao;
import com.lucky.ebidding.dao.TbInfoDao;
import com.lucky.ebidding.dao.UserDao;
import com.lucky.ebidding.dao.ZbInfoDao;
import com.lucky.ebidding.dto.TbAndZbDTO;
import com.lucky.ebidding.dto.TbAndZbIdDTO;
import com.lucky.ebidding.enums.NotificationTypeEnum;
import com.lucky.ebidding.model.*;
import com.lucky.ebidding.service.interfaces.PublishServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:PublishService
 * Package:com.lucky.ebidding.service
 * Description:
 *
 * @Date:2020/5/21 14:51
 * @Author:857251389@qq.com
 */
@Service
public class PublishService implements PublishServiceI {

    @Autowired
    private PublishInfoDao publishInfoDao;

    @Autowired
    private NotificationService notificationService;

    @Autowired
    private ZbInfoDao zbInfoDao;

    @Autowired
    private TbInfoDao tbInfoDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private TbService tbService;

    @Autowired
    private ZbService zbService;

    @Override
    public List<PublishInfo> selectAll() {

        List<PublishInfo> publishInfos = publishInfoDao.selectByExample(new PublishInfoExample());
        return publishInfos;
    }

    @Override
    public int setStatus(Integer status, Integer publishId, Integer zbId, Integer tbUserId, User user) {
        PublishInfo publishInfo = new PublishInfo();
        publishInfo.setId(publishId);
        publishInfo.setPublishShFlag(status);
        int i = publishInfoDao.updateByPrimaryKeySelective(publishInfo);
        //审核通过，发送通知
        if (status == 1) {
            if (i != 0) {
                notificationService.sendNotification(tbUserId, user.getId(), NotificationTypeEnum.SUCCESS_ZB.getType(), zbId, "恭喜您");
            }
            PublishInfo publishInfo1 = publishInfoDao.selectByPrimaryKey(publishId);
            List<TbInfo> tbInfos = tbService.selectTbInfoById(publishInfo1.getZbId());
            List<TbInfo> collect = tbInfos.stream().filter(tbInfo -> tbInfo.getId().equals(publishInfo1.getTbId())).collect(Collectors.toList());
            tbInfos.remove(collect.get(0));
            for (TbInfo tbInfo : tbInfos) {
                notificationService.sendNotification(tbInfo.getUserId(), user.getId(), NotificationTypeEnum.FAILED_ZB.getType(), zbId, "已开标");
            }

            zbService.uppdateKbFlag(zbId);
        }
        return i;
    }

    @Override
    public TbAndZbDTO selectZbInfoAndTbInfoByPublishId(Integer publishId) {

        PublishInfo publishInfo = publishInfoDao.selectByPrimaryKey(publishId);

        ZbInfo zbInfo = zbInfoDao.selectByPrimaryKey(publishInfo.getZbId());
        TbInfo tbInfo = tbInfoDao.selectByPrimaryKey(publishInfo.getTbId());
        User user = userDao.selectByPrimaryKey(tbInfo.getUserId());
        TbAndZbDTO tbAndZbDTO = new TbAndZbDTO();
        tbAndZbDTO.setZbInfo(zbInfo);
        tbAndZbDTO.setTbInfo(tbInfo);
        tbAndZbDTO.setTbUser(user);
        return tbAndZbDTO;
    }

    @Override
    public TbAndZbDTO selectZbInfoAndTbInfoByzbId(Integer zbId) {
        PublishInfoExample publishInfoExample = new PublishInfoExample();
        publishInfoExample.createCriteria().andZbIdEqualTo(zbId);
        List<PublishInfo> publishInfos = publishInfoDao.selectByExample(publishInfoExample);

        ZbInfo zbInfo = zbInfoDao.selectByPrimaryKey(zbId);
        TbInfo tbInfo = tbInfoDao.selectByPrimaryKey(publishInfos.get(0).getTbId());
        User user = userDao.selectByPrimaryKey(tbInfo.getUserId());
        TbAndZbDTO tbAndZbDTO = new TbAndZbDTO();
        tbAndZbDTO.setZbInfo(zbInfo);
        tbAndZbDTO.setTbInfo(tbInfo);
        tbAndZbDTO.setTbUser(user);
        return tbAndZbDTO;
    }
}
